<template>
  <div class="settings-form">
    <div class="profile-section">
      <el-form :model="profileForm" ref="profileFormRef" :rule="rules" label-width="80px" label-position="top">
        <el-form-item label="头像" prop="avatar_url">
          <div class="avatar-section">
            <div class="avatar-wrapper">
              <el-avatar :size="100" :src="profileForm.avatar_url"></el-avatar>
              <div class="avatar-upload">
                <el-upload
                  class="avatar-uploader"
                  action="/lingxi/upload/file"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                >
                  <el-button plain>更换照片</el-button>
                </el-upload>
                <p class="upload-hint">建议尺寸800×800像素</p>
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="姓名" prop="nickname">
          <el-input v-model="profileForm.nickname" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="profileForm.phone" disabled placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="profileForm.phone" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="remark">
          <el-input
              type="textarea"
              v-model="profileForm.remark"
              :rows="4"
              placeholder="自我介绍"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updateProfile">更新</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import {mapState} from "vuex";
import {http} from "@/api";
export default {
  name: "index",
  data() {
    return {
      profileForm: {
        id: this.$store.state.user.userInfo.id,
        avatar_url: '',
        nickname: '',
        phone: '',
        remark: ''
      },
      rules: {
        nickname: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
      }
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  created() {
    this.initProfileForm()
  },
  methods: {
    initProfileForm() {
      this.profileForm = {
        id: this.userInfo.id,
        nickname: this.userInfo.nickname,
        phone: this.userInfo.phone,
        remark: this.userInfo.remark,
        phone: this.userInfo.phone,
        avatar_url: this.userInfo.avatar_url
      }
    },
    async updateProfile() {
      try {
        const valid = await this.$refs.profileFormRef.validate()
        if (valid) {
          await this.$store.dispatch('user/updateProfile', this.profileForm)
          this.$message.success('更新成功')
        }
      } catch (error) {
        console.log(error)
        this.$message.error('更新失败')
      }
    },
    handleAvatarSuccess(response) {
      if (response.success) {
        // 更新用户头像URL
        this.profileForm.avatar_url = response.data.url;
      } else {
        this.$message.error('头像上传失败');
      }
    },
    beforeAvatarUpload(file) {
      // 检查文件类型
      const isImage = ['image/jpeg', 'image/png', 'image/gif'].includes(file.type);
      if (!isImage) {
        this.$message.error('头像必须是图片格式!');
        return false;
      }

      // 检查文件大小（限制为2MB）
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (!isLt2M) {
        this.$message.error('头像大小不能超过5MB!');
        return false;
      }

      return true;
    },
    async updateUserInfo(data) {
      try {
        await http.user.update(this.profileForm.id, data);
        this.$message.success('更新成功');
      } catch (error) {
        this.$message.error(error.message || '更新失败');
      }
    }
  }
}
</script>

<style scoped lang="scss">
.settings-form {
  padding: 0px 48px;
  overflow: auto;
  .profile-section {
    .avatar-section {
      .avatar-wrapper {
        display: flex;
        align-items: center;
        flex-direction: column;

        .avatar-upload {
          margin-top: 20px;
          text-align: center;

          .upload-hint {
            margin-top: 8px;
            font-size: 12px;
            color: #999;
          }
        }
      }
    }

    .el-form {
      max-width: 500px;
    }
  }
}

:deep(.el-upload) {
  width: 100%;
}
</style>
